-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.



function refDeleted(deleted)
	ref = nil;
end

function refTargeted(targeted)
	if window.targeting then
		window.targeting.update(ref);
	end
end

function refDrop(token, draginfo)
	if window.handleTokenDrop then
		local r = window.handleTokenDrop(token, draginfo);
		if r then
			draginfo.reset();	-- prevent multiple adds
		end
		return r;
	end
end

function setActive(status)
	if ref then
		ref.setActive(status);
	end
end

function updateUnderlay()
	if ref then
		ref.removeAllUnderlays();

		local spacesquares, reachsquares, gmonlyreach;
		if window.getTokenUnderlayProperties then
			spacesquares, reachsquares, gmonlyreach = window.getTokenUnderlayProperties();
		end
		
		if reachsquares then
			if gmonlyreach then
				ref.addUnderlay(reachsquares, "4f000000", "hover,gmonly");
			else
				ref.addUnderlay(reachsquares, "4f000000", "hover");
			end
		end
		
		if spacesquares then
			if getFoF() == "friend" then
				ref.addUnderlay(spacesquares, "2f00ff00");
			elseif getFoF() == "foe" then
				ref.addUnderlay(spacesquares, "2fff0000");
			elseif getFoF() == "neutral" then
				ref.addUnderlay(spacesquares, "2fffff00");
			end
		end
	end
end

function acquireReference(dropref, override)
	if dropref then
		if ref and ref ~= dropref then
			ref.delete();
		end

		ref = dropref;
		
		ref.onDelete = refDeleted;
		ref.onTargetUpdate = refTargeted;
		ref.onDrop = refDrop;

		ref.setActivable(true);
		ref.setTargetable(true);

		local visibility, ismodifiable;
		if window.getTokenPermissions then
			visibility, ismodifiable = window.getTokenPermissions();
		end

		ref.setVisible(visibility);
		ref.setModifiable(ismodifiable);
			
		ref.setActive(window.active.getState());
		ref.setName(namenode.getValue());

		updateUnderlay();
		
		if override then
			ref.setScale(getScale());
		else
			setScale(ref.getScale(), true);
		end
		
		-- Store into database
		tokenrefnode.setValue(ref.getContainerNode().getNodeName());
		tokenrefidnode.setValue(ref.getId());
		
		return true;
	end
end

function setScale(scale, internal)
	if not scaleWidget then		
		scaleWidget = addTextWidget("sheetlabelsmall", "0");
		scaleWidget.setFrame("tempmodmini", 4, 1, 6, 3);
		scaleWidget.setPosition("topright", -2, 2);
	end
	
	scale = math.floor(scale*10)/10;

	if scale == 1 then
		scaleWidget.setVisible(false);
	else
		scaleWidget.setVisible(true);
		scaleWidget.setText(scale);
	end
	
	if ref then
		ref.setScale(scale);
	end

	if not internal then
		scalenode.setValue(scale);
	end
end

function getScale()
	return scalenode.getValue();
end

function updateScale()
	setScale(scalenode.getValue(), true);
end

function getFoF()
	if not fofnode then
		return "";
	end
	
	return fofnode.getValue();
end

function updateFoF()
	updateUnderlay();
end

function updateName()
	if ref then
		ref.setName(namenode.getValue());
	end
end

function onInit()
	-- Name
	namenode = window.getDatabaseNode().createChild("name", "string");
	namenode.onUpdate = updateName;
	
	-- Check for an existing token instance
	tokenrefnode = window.getDatabaseNode().createChild(getName() .. "_refnode", "string");
	tokenrefidnode = window.getDatabaseNode().createChild(getName() .. "_refid", "number");
	
	if tokenrefnode.getValue() ~= "" and tokenrefidnode.getValue() then
		local imageinstance = populateFromImageNode(tokenrefnode.getValue(), tokenrefidnode.getValue());
		if imageinstance then
			acquireReference(imageinstance);
		end
	end
	
	-- Scale
	scalenode = window.getDatabaseNode().createChild(getName() .. "_scale", "number");
	if scalenode.getValue() == 0 then
		setScale(1.0);
	else
		setScale(scalenode.getValue(), true);
	end
	scalenode.onUpdate = updateScale;
	
	-- Monitor FoF
	fofnode = window.getDatabaseNode().createChild("friendfoe", "string");
	fofnode.onUpdate = updateFoF;
end

function onDrop(x, y, draginfo)
	if draginfo.isType("token") then
		local prototype, dropref = draginfo.getTokenData();
		setPrototype(prototype);
		return acquireReference(dropref);
	end
end

function onDragEnd(draginfo)
	local prototype, dropref = draginfo.getTokenData();
	return acquireReference(dropref, true);
end

function onClickDown(button, x, y)
	return true;
end

function onClickRelease(button, x, y)
	if button == 1 then
		if ref then
			if ref.isActive() then
				ref.setActive(false);
			else
				ref.setActive(true);
			end
		end
	else
		setScale(1.0)
	end
	
	return true;
end

function onWheel(notches)
	local scale = 1.0;

	if Input.isControlPressed() then
		scale = math.floor(getScale() + notches);
		if scale < 1 then
			scale = 1;
		end
	else
		scale = getScale() + notches*0.1;

		if scale < 0.1 then
			scale = 0.1;
		end
	end

	setScale(scale);
		
	return true;
end